//
//  addTableViewController.m
//  CarTown
//
//  Created by xionghao on 15/8/28.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "addTableViewController.h"
#import "PickerTableViewCell.h"
#import "selectView.h"
#import "PikerImageTableViewCell.h"
#import "CarpubMode.h"
#import "Pubmode.h"
#import "IWUploadParam.h"
#import "EidtTableViewCell.h"
#import "PublishTopTableViewCell.h"
#import "RadioTableViewCell.h"
#import "AltuoPriceTableViewCell.h"
#import <QBImagePickerController/QBImagePickerController.h>
#import "PickViewPhotoTableViewCell.h"
#import "CarSourceType.h"
#import "CarsrouceTypePickerController.h"
#import "CarPickModel.h"
#import "CarDetail.h"
#import "ColorPickViewController.h"
#import "FindCarTableViewController.h"
#import "JianjieViewController.h"
#import "LongPeizhi.h"
#import "AddCarPeizhiTableViewCell.h"
#import "CarDetailTableViewCell.h"
#import "PostResdemode.h"
#import "PostSUSViewController.h"
@interface addTableViewController ()
<SelectCellDelegate,ImagePickerBtnDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,QBImagePickerControllerDelegate,PickerViewCellDelegate,EidtTableViewCellDelegate,ColorPikerDelegate,PostSUSViewControllerDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) selectView *selectview;
@property (nonatomic,strong) UIImagePickerController *picker;
@property (nonatomic,strong) NSMutableArray *array;
@property (nonatomic,strong) NSMutableArray *setetionarray;
@property (nonatomic,strong) PikerImageTableViewCell *currtcell;
@property (nonatomic,strong) CarpubMode *photomode;
@property (nonatomic,strong) UIButton *button;
@property (nonatomic,strong)  Pubmode *leixing;
@property (nonatomic,strong) LongPeizhi *peizhi;
@property (nonatomic,strong) Pubmode *waiguan;
@property (nonatomic,strong) Pubmode *neishi;
@property (nonatomic,strong) CarPickModel *chexing;
@property (nonatomic,strong) Pubmode *cheshen;
@property (nonatomic,strong) Pubmode *paifang;
@property (nonatomic,strong) Pubmode *name;
@property (nonatomic,strong) Pubmode *zonghe;
@property (nonatomic,strong) Pubmode *gouzhi;
@property (nonatomic,strong) Pubmode *shangpai;
@property (nonatomic,strong) Pubmode *qita;
@property (nonatomic,strong) Pubmode *youhui;
@property (nonatomic,strong) Pubmode *quanbao;
@property (nonatomic,strong) Pubmode *heji;
@property (nonatomic,assign)  NSInteger currpostion;
@property (nonatomic,copy)  NSString *csid;
@property (nonatomic,strong) Puser *user;
@property (nonatomic,strong) CarpubMode *carmode;
@property (nonatomic,strong) CarpubMode *beizhu;
@property (nonatomic,strong) UITableView *tableView;
@end

@implementation addTableViewController
+(instancetype)Viewcontroller{
    addTableViewController *addvc=[[addTableViewController alloc]init];
    addvc.hidesBottomBarWhenPushed=YES;
    return addvc;
}
-(Puser *)user{
    if (_user==nil) {
        _user=[Puser getUser];
    }
    return _user;
}
-(UITableView *)tableView{
    if (_tableView==nil) {
        _tableView=[[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}
-(NSMutableArray *)array{
    if (_array==nil) {
        _array=[NSMutableArray array];
    }
    return _array;
}
-(NSMutableArray *)setetionarray{
    if (_setetionarray==nil) {
        _setetionarray=[NSMutableArray array];
    }
    return _setetionarray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.backgroundColor=bgGarycolor;
    self.navigationItem.title=self.carmode? @"修改展车":@"添加展车";
    UINib *top=[UINib nibWithNibName:NSStringFromClass([PublishTopTableViewCell class]) bundle:nil];
    UINib *radio=[UINib nibWithNibName:NSStringFromClass([RadioTableViewCell class]) bundle:nil];
    UINib *edit=[UINib nibWithNibName:NSStringFromClass([EidtTableViewCell class]) bundle:nil];
    UINib *img=[UINib nibWithNibName:NSStringFromClass([PikerImageTableViewCell class]) bundle:nil];
    [self.tableView registerNib:top  forCellReuseIdentifier:@"top"];
    [self.tableView registerNib:edit  forCellReuseIdentifier:@"editcell"];
    [self.tableView registerNib:radio  forCellReuseIdentifier:@"radio"];
    [self.tableView registerNib:img  forCellReuseIdentifier:@"img"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getcarname:) name:NoticePostGetCar object:nil];
    [self setupdata];
    [self setuppostbtn];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        
    }
    if (self.isFromSUS) {
        NSMutableArray *vcarray=[[NSMutableArray alloc] initWithArray: self.navigationController.viewControllers];
        [vcarray removeObjectAtIndex:self.navigationController.viewControllers.count-2];
        [vcarray removeObjectAtIndex:self.navigationController.viewControllers.count-3];
        self.navigationController.viewControllers = vcarray;
    }
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

-(void)continueClick{
    self.mode=nil;
    self.cheshen=nil;
    [self setupdata];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
        
    }
    
}
-(void)textFieldChange:(EidtTableViewCell *)cell{
    NSIndexPath *indexpath=[self.tableView indexPathForCell:cell];
    if ((indexpath.section==1&&indexpath.row>1&&indexpath.row<6)||(indexpath.section==1&&indexpath.row==7)) {
        NSInteger heji=[self.cheshen.detail integerValue]+[self.gouzhi.detail integerValue]+[self.shangpai.detail integerValue]+[self.qita.detail integerValue];
        self.heji.detail=[NSString stringWithFormat:@"%ld",heji];
        NSInteger youhui=heji-[self.youhui.detail integerValue];
        NSMutableArray *marray=[NSMutableArray array];
        NSIndexPath *indexpath=[NSIndexPath indexPathForRow:6 inSection:1];
        NSIndexPath *indexpath2=[NSIndexPath indexPathForRow:8 inSection:1];
        [marray addObject:indexpath];
        [marray addObject:indexpath2];
        if (youhui<0) {
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.labelText=@"综合优惠不能大于合计价格";
            hud.mode = MBProgressHUDModeText;
            hud.margin = 10.f;
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:1];
            self.youhui.detail=nil;
            NSIndexPath *indexpath3=[NSIndexPath indexPathForRow:7 inSection:1];
            [marray addObject:indexpath3];
            
        }else{
            self.quanbao.detail=[NSString stringWithFormat:@"%ld",youhui];
        }
        
        
        
        [self.tableView reloadRowsAtIndexPaths:marray withRowAnimation:UITableViewRowAnimationAutomatic];
        
    }
}



-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)clearother{
    self.chexing.cardetail=nil;
    self.chexing.brand=nil;
    self.chexing.carporgchild=nil;
    self.cheshen.detail=nil;
    self.name.name=@"选择车型后自动生成";
    self.gouzhi.detail=nil;
    self.shangpai.detail=nil;
    self.zonghe.detail=nil;
    self.qita.detail=nil;
    self.heji.detail=nil;
    self.youhui.detail=nil;
    self.quanbao.detail=nil;
    self.beizhu.detail=nil;
    self.chexing.detail=@"请选择";
    self.peizhi.loogdetail=nil;
    self.paifang.radioindex=-1;
    [self.carmode.imgarray removeAllObjects];
    self.neishi.detail=@"请选择";
    self.waiguan.detail=@"请选择";
    
}
-(void)setupdata{
    
    [self.setetionarray removeAllObjects];
    [self.array removeAllObjects];
    
    Pubmode *leixing=[[Pubmode alloc]init];
    leixing.name=@"车源类型";
    
    if (self.mode) {
        //        CarSourceType *type=[CarSourceType getcarsoucebyID:self.mode.type_id];
        leixing.detail=self.mode.type_name;
        //        CarSourceType *type= [CarSourceType getcarsoucebyText:leixing.detail];
        self.csid=self.mode.type_id;
        
    }else{
        leixing.detail=@"请选择";
    }
    leixing.block=^{
        CarsrouceTypePickerController *picker=[CarsrouceTypePickerController picker];
        picker.currtpostion=self.currpostion;
        picker.title=self.leixing.name;
        picker.block=^(NSString *str,NSString *cs_id,NSInteger currtpostion){
            
            if ([self.leixing.detail isEqualToString:str]) {
                
            }else{
                [self clearother];
            }
            self.leixing.detail=str;
            self.currpostion=currtpostion;
            self.csid=cs_id;
            [self.tableView reloadData];
            
        };
        
        [self.navigationController pushViewController:picker animated:YES];
    };
    leixing.isMust=YES;
    self.leixing=leixing;
    
    CarPickModel *chexing=[[CarPickModel alloc]init];
    chexing.name=@"车型";
    chexing.isMust=YES;
    if (self.mode) {
        chexing.detail=self.mode.cat_name;
        CarDetail *cardetail=[[CarDetail alloc]init];
        cardetail.ID=self.mode.mid;
        cardetail.name=self.mode.cat_name;
        chexing.cardetail=cardetail;
        CarbrandModel *brand=[[CarbrandModel alloc]init];
        brand.brand_id=self.mode.brand_id;
        brand.brand_name=self.mode.brand_name;
        chexing.brand=brand;
        
    }else{
        chexing.detail=@"请选择";
    }
    chexing.block=^{
        if ([self.leixing.detail isEqualToString:@"请选择"]) {
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.labelText=@"请先选择车源类型";
            hud.mode = MBProgressHUDModeText;
            hud.margin = 10.f;
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:1];
        }else{
            FindCarTableViewController *fincar=[FindCarTableViewController findcar];
            fincar.type=self.csid;
            [self.navigationController pushViewController:fincar animated:YES];
        }
    };
    self.chexing=chexing;
    
    Pubmode *waiguan=[[Pubmode alloc]init];
    waiguan.name=@"外观颜色";
    waiguan.isMust=YES;
    if (self.mode) {
        waiguan.detail=self.mode.car_outside_color;
    }else{
        waiguan.detail=@"请选择";
    }
    waiguan.block=^{
        
        if ([self.chexing.detail isEqualToString:@"请选择"]) {
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.labelText=@"请先选择车型";
            hud.mode = MBProgressHUDModeText;
            hud.margin = 10.f;
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:1];
        }else{
            
            ColorPickViewController *color=[ColorPickViewController colorpicker];
            color.delegate=self;
            
            color.ID=self.chexing.cardetail.ID;
            color.index=self.waiguan.index;
            color.colorstr=self.waiguan.detail;
            color.isOut=YES;
            color.view.tag=0;
            [self.navigationController pushViewController:color animated:YES];
        }
    };
    self.waiguan=waiguan;
    Pubmode *neishi=[[Pubmode alloc]init];
    neishi.name=@"内饰";
    neishi.isMust=YES;
    neishi.detail=self.mode?self.mode.car_inside_color:@"请选择";
    self.neishi=neishi;
    neishi.block=^{
        if ([self.chexing.detail isEqualToString:@"请选择"]) {
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.labelText=@"请先选择车型";
            hud.mode = MBProgressHUDModeText;
            hud.margin = 10.f;
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:1];
        }else{
            ColorPickViewController *color=[ColorPickViewController colorpicker];
            color.delegate=self;
            color.ID=self.chexing.cardetail.ID;
            color.index=self.neishi.index;
            color.colorstr=self.waiguan.detail;
            color.isOut=NO;
            color.view.tag=1;
            [self.navigationController pushViewController:color animated:YES];
        }
    };
    self.neishi=neishi;
    Pubmode *paifang=[[Pubmode alloc]init];
    paifang.name=@"排放要求";
    
    paifang.titlearray=@[@"国四",@"国五",@"不限"];
    
    switch ([self.mode.standard integerValue]) {
        case 4:
            paifang.radioindex=0;
            break;
        case 5:
            paifang.radioindex=1;
            break;
        case 0:
            paifang.radioindex=2;
            break;
        default:
            break;
    }
    
    self.paifang=paifang;
    LongPeizhi *peizhi=[[LongPeizhi alloc]init];
    peizhi.name=@"配置";
    peizhi.loogdetail=![self.mode.car_configure isEqualToString:@""]&&self.mode?self.mode.car_configure:@"请输入";
    peizhi.block=^{
        JianjieViewController *jjvc=[JianjieViewController viewcontroller];
        jjvc.textlength=500;
        jjvc.navtitle=peizhi.name;
        jjvc.text=[peizhi.loogdetail isEqualToString:@"请输入"]?@"":peizhi.loogdetail;
        jjvc.block=^(NSString *str){
            if (str==nil) {
                peizhi.loogdetail=@"请输入";
            }else{
                peizhi.loogdetail=str;
            }
            
            
            NSIndexPath *indepath=[NSIndexPath indexPathForRow:5 inSection:0];
            [self.tableView reloadRowsAtIndexPaths:@[indepath] withRowAnimation:UITableViewRowAnimationAutomatic];
        };
        [self.navigationController pushViewController:jjvc animated:YES];
    };
    
    self.peizhi=peizhi;
    CarpubMode *photomode=[[CarpubMode alloc]init];
    photomode.name=@"车辆图片";
    photomode.imgarray=[NSMutableArray array];
    if (self.mode) {
        photomode.isHasURL=YES;
        photomode.urlarray=self.mode.pic;
    }
    self.carmode=photomode;
    [self.setetionarray addObject:leixing];
    [self.setetionarray addObject:chexing];
    [self.setetionarray addObject:waiguan];
    [self.setetionarray addObject:neishi];
    
    [self.setetionarray addObject:paifang];
    [self.setetionarray addObject:peizhi];
    [self.setetionarray addObject:photomode];
    
    
    Pubmode *name=[[Pubmode alloc]init];
    name.name=self.mode?self.mode.cat_name:@"选择车型后自动生成";
    name.isNormal=YES;
    self.name=name;
    Pubmode *cheshen=[[Pubmode alloc]init];
    cheshen.name=@"车身价指导价";
    cheshen.isEidt=YES;
    cheshen.isMust=YES;
    cheshen.limitlength=5;
    if (self.mode) {
        cheshen.detail=self.mode.price_zdj;
    }
    cheshen.placehoder=@"请输入指导价";
    cheshen.pricetitle=@"元";
    cheshen.yuancolor=tintblue;
    self.cheshen=cheshen;
    Pubmode *gouzhi=[[Pubmode alloc]init];
    gouzhi.name=@"购置税";
    gouzhi.placehoder=@"请输入购置税金额";
    gouzhi.isEidt=YES;
    if (self.mode) {
        gouzhi.detail=self.mode.price_gzs;
    }
    gouzhi.pricetitle=@"元";
    self.gouzhi=gouzhi;
    Pubmode *shangpai=[[Pubmode alloc]init];
    shangpai.name=@"上牌费";
    shangpai.isEidt=YES;
    shangpai.pricetitle=@"元";
    if (self.mode) {
        shangpai.detail=self.mode.price_spf;
    }
    shangpai.placehoder=@"请输入上牌费金额";
    self.shangpai=shangpai;
    Pubmode *zonghe=[[Pubmode alloc]init];
    zonghe.name=@"综合险";
    zonghe.isEidt=YES;
    zonghe.pricetitle=@"元";
    if (self.mode) {
        zonghe.detail=self.mode.price_zhx;
    }
    zonghe.placehoder=@"请输入综合险金额";
    self.zonghe=zonghe;
    Pubmode *qita=[[Pubmode alloc]init];
    qita.name=@"其他费用";
    qita.isEidt=YES;
    qita.placehoder=@"0";
    qita.pricetitle=@"元";
    qita.detail=self.mode.price_other;
    self.qita=qita;
    Pubmode *heji=[[Pubmode alloc]init];
    heji.name=@"合计";
    heji.placehoder=@"自动生成";
    heji.pricetitle=@"元";
    heji.isEidt=YES;
    heji.yuancolor=tintblue;
    heji.isAluto=YES;
    heji.detail=self.mode.sysprice;
    self.heji=heji;
    Pubmode *zongheyouhui=[[Pubmode alloc]init];
    zongheyouhui.name=@"综合优惠";
    zongheyouhui.isEidt=YES;
    zongheyouhui.placehoder=@"请输入综合优惠金额";
    zongheyouhui.isMust=YES;
    zongheyouhui.pricetitle=@"元";
    zongheyouhui.detail=self.mode.yhprice;
    self.youhui=zongheyouhui;
    Pubmode *quanbao=[[Pubmode alloc]init];
    quanbao.name=@"最终价格";
    quanbao.placehoder=@"自动生成";
    quanbao.pricetitle=@"元";
    quanbao.yuancolor=tintblue;
    quanbao.isAluto=YES;
    quanbao.isEidt=YES;
    quanbao.detail=self.mode.allprice;
    self.quanbao=quanbao;
    Pubmode *beizhu=[[Pubmode alloc]init];
    beizhu.name=@"备注";
    beizhu.detail=self.mode.remark&&![self.mode.remark isEqualToString:@""]?self.mode.remark:@"请输入";
    beizhu.block=^{
        JianjieViewController *jjvc=[JianjieViewController viewcontroller];
        jjvc.textlength=500;
        jjvc.navtitle=beizhu.name;
        jjvc.text=[beizhu.detail isEqualToString:@"请输入"]?@"":beizhu.detail;
        jjvc.block=^(NSString *str){
            if (str==nil) {
                beizhu.detail=@"请输入";
            }else{
                beizhu.detail=str;
            }
            NSIndexPath *indepath=[NSIndexPath indexPathForRow:9 inSection:1];
            [self.tableView reloadRowsAtIndexPaths:@[indepath] withRowAnimation:UITableViewRowAnimationAutomatic];
        };
        [self.navigationController pushViewController:jjvc animated:YES];    };
    self.beizhu=beizhu;
    NSArray *array=@[name,cheshen,gouzhi,shangpai,zonghe,qita,heji,zongheyouhui,quanbao,beizhu];
    
    [self.array addObject:self.setetionarray];
    [self.array addObject:array];
    [self.tableView reloadData];
    if (self.array.count>0) {
        NSIndexPath *indexpath=[NSIndexPath indexPathForRow:0 inSection:0];
        [self.tableView scrollToRowAtIndexPath:indexpath atScrollPosition:UITableViewScrollPositionNone animated:NO];
    }
}

#pragma -mark 车型选择回调
-(void)getcarname:(NSNotification *)notice{
    CarDetail *detail=notice.userInfo[@"cardetail"];
    CarbrandModel *brand=notice.userInfo[@"brand"];
    CarprogChildmode *carprogchild=notice.userInfo[@"cartory"];
    self.chexing.detail=detail.name;
    self.chexing.cardetail=detail;
    self.chexing.carporgchild=carprogchild;
    self.chexing.brand=brand;
    
    if ([self.leixing.detail isEqualToString:@"中规"]) {
        self.cheshen.detail=[NSString stringWithFormat:@"%.0f",[self.chexing.cardetail.price floatValue]*10000];
    }else{
        self.cheshen.detail=nil;
    }
    self.gouzhi.detail=nil;
    self.shangpai.detail=nil;
    self.zonghe.detail=nil;
    self.qita.detail=nil;
    self.heji.detail=nil;
    self.youhui.detail=nil;
    self.quanbao.detail=nil;
    self.beizhu.detail=nil;
    self.name.name=self.chexing.cardetail.name;
    self.peizhi.loogdetail=nil;
    self.paifang.radioindex=-1;
    [self.carmode.imgarray removeAllObjects];
    self.neishi.detail=@"请选择";
    self.waiguan.detail=@"请选择";
    
    [self.tableView reloadData];
    
    
}


#pragma -mark 颜色选择回调
-(void)ColorPikcerClickContoller:(ColorPickViewController *)picker withIndex:(NSInteger)index ColorStr:(NSString *)colorstr{
    NSIndexPath *indexpath;
    if(picker.view.tag==0){
        self.waiguan.detail=colorstr;
        self.waiguan.index=index;
        indexpath=[NSIndexPath indexPathForRow:2 inSection:0];
        
    }else{
        self.neishi.detail=colorstr;
        self.neishi.index=index;
        indexpath=[NSIndexPath indexPathForRow:3 inSection:0];
    }
    [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
-(void)setuppostbtn{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, XHScreenwidth, 64)];
    UIButton *postbtn=[[UIButton alloc]initWithFrame:CGRectMake(16, 16, XHScreenwidth-32, 36)];
    [postbtn setBackgroundImage:[UIImage imageNamed:@"bottom_bg"] forState:UIControlStateNormal];
    NSString *btnstr;
    if (self.mode) {
        btnstr=@"确定保存";
    }else{
        btnstr=@"确定添加";
    }
    [postbtn setTitle:btnstr forState:UIControlStateNormal];
    [postbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [postbtn addTarget:self action:@selector(postclick) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:postbtn];
    self.tableView.tableFooterView=view;
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
-(void)photoCellView:(PickViewPhotoTableViewCell *)cell pickerBtnClick:(NSInteger)index withButton:(UIButton *)btn{
    self.button=btn;
    self.currtcell=cell;
    
    if (index==0) {
        UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera;
        self.picker.sourceType=sourcheType;
        [self presentViewController:self.picker animated:YES completion:nil];
    }else{
        
        
        NSIndexPath *indexpath=[self.tableView indexPathForCell:cell];
        NSArray *array=self.array[indexpath.section];
        CarpubMode *photomode=array[indexpath.row];
        
        
        QBImagePickerController *imagePickerController = [QBImagePickerController new];
        imagePickerController.delegate = self;
        imagePickerController.allowsMultipleSelection = YES;
        if (btn.isSelected) {
            imagePickerController.maximumNumberOfSelection = 1;
        }else{
            imagePickerController.maximumNumberOfSelection = 8-photomode.imgarray.count;
        }
        imagePickerController.showsNumberOfSelectedAssets = YES;
        
        
        [self presentViewController:imagePickerController animated:YES completion:NULL];
    }
}

-(UIImagePickerController *)picker{
    if (_picker==nil) {
        _picker=[[UIImagePickerController alloc]init];
        _picker.view.backgroundColor = [UIColor whiteColor];
        
        _picker.delegate =self;
        _picker.allowsEditing = YES;
    }
    return _picker;
}

-(void)qb_imagePickerController:(QBImagePickerController *)imagePickerController didSelectAssets:(NSArray *)assets{
    NSMutableArray *marray=[NSMutableArray array];
    
    [self dismissViewControllerAnimated:YES completion:^{
        for (ALAsset *asset in assets) {
            
            UIImage *image=[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]];
            [marray addObject:image];
            
        }
        NSIndexPath *indexpath=[self.tableView indexPathForCell:self.currtcell];
        NSArray *array=self.array[indexpath.section];
        CarpubMode *photomode=array[indexpath.row];
        photomode.ischange=YES;
        if (self.button.isSelected) {
            [photomode.imgarray replaceObjectAtIndex:self.button.tag withObject:marray[0]];
        }else{
            
            [photomode.imgarray addObjectsFromArray:marray];
        }
        [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }];
    
}
-(void)photoCellViewReloadImage:(PickViewPhotoTableViewCell *)cell{
    NSIndexPath *indexpath=[self.tableView indexPathForCell:cell];
    
    [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (void)qb_imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController {
    [self dismissViewControllerAnimated:YES completion:NULL];
}

-(void)postclick{
    
    if ([self checkFrom]) {
        
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"正在添加...";
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        
        NSMutableDictionary *params=[NSMutableDictionary dictionary];
        if (self.mode) {
            params[@"action"]=@"edit_car";
            params[@"id"]=self.mode.ID;
        }else{
            params[@"action"]=@"add_display";
        }
        NSInteger power=[self.user.power integerValue];
        if (power<5) {
            params[@"type"]=@"person";
            params[@"user_id"]=self.user.user_id;
        }else{
            params[@"type"]=@"company";
            params[@"user_id"]=self.user.com_id;
            
        }
        CarSourceType *type=[CarSourceType getcarsoucebyText:self.leixing.detail];
        params[@"type_id"]=type.csid;
        params[@"mid"]=self.chexing.cardetail.ID;
        params[@"brand_id"]=self.chexing.brand.brand_id;
        params[@"cat_name"]=self.chexing.cardetail.name;
        params[@"price_zdj"]=self.cheshen.detail;
        params[@"yhprice"]=self.youhui.detail;
        params[@"car_inside_color"]=self.neishi.detail;
        params[@"car_outside_color"]=self.waiguan.detail;
        if (self.paifang.radioindex!=-1) {
            switch (self.paifang.radioindex) {
                case 0:
                    params[@"standard"]=@"4";
                    break;
                case 1:
                    params[@"standard"]=@"5";
                    break;
                case 2:
                    params[@"standard"]=@"0";
                    break;
                default:
                    break;
            }
            
        }
        if (![self.peizhi.loogdetail isEqualToString:@"请输入"]) {
            params[@"car_configure"]=self.peizhi.loogdetail;
        }
        
        if ([self.gouzhi.detail integerValue]!=0) {
            params[@"price_gzs"]=self.gouzhi.detail;
        }
        if ([self.shangpai.detail integerValue]!=0) {
            params[@"price_spf"]=self.shangpai.detail;
        }
        if ([self.zonghe.detail integerValue]!=0) {
            params[@"price_zhx"]=self.zonghe.detail;
        }if ([self.qita.detail integerValue]!=0) {
            params[@"price_other"]=self.qita.detail;
        }
        if (![self.beizhu.detail isEqualToString:@"请输入"]&&self.beizhu.detail) {
            params[@"remark"]=self.beizhu.detail;
        }
        params[@"allprice"]=self.quanbao.detail;
        params[@"sysprice"]=self.heji.detail;
        
        if (self.carmode.imgarray.count>0&&self.carmode.ischange) {
            
            NSMutableArray *imgstrarray=[NSMutableArray array];
            
            for (UIImage *image in self.carmode.imgarray) {
                NSData *imagedata=[UIImage compressData:image toMaxFileSize:DefailutPicSize];
                NSString *base64str=[imagedata imageBase64Data:imagedata];
                [imgstrarray addObject:base64str];
            }
            NSString *imgp=[NSString DataTOjsonString:imgstrarray];
            params[@"pic"]=imgp;
        }else {
            if (self.carmode.urlarray.count>0&&!self.carmode.ischange) {
                params[@"pic"]=[NSString DataTOjsonString:self.carmode.urlarray];
            }
        }
        [PostUitls Post:myzturl parameters:params success:^(id response) {
            PostResdemode *resd =[PostResdemode objectWithKeyValues:response];
            
            if ([resd.state integerValue]==400) {
                PostSUSViewController *svc=[PostSUSViewController viewcontroller];
                svc.delegate=self;
                if (self.mode) {
                    svc.type=POSTADDZTCARFROMZTEidt;
                    svc.ID=self.mode.ID;
                }else{
                    svc.type=POSTADDZTCARFROMZT;
                    svc.ID=[NSString stringWithFormat:@"%ld",[resd.data integerValue]];
                }
                
                
                [self.navigationController pushViewController:svc animated:YES];
            }else{
                hud.labelText=resd.msg;
            }
            [hud hide:YES];
            
            
            
        } failure:^(NSError *error) {
            
        }];
        
        
    }
    
}


-(BOOL)checkFrom{
    
    if ([self.leixing.detail isEqualToString:@"请选择"]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请选择车源类型";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    
    if ([self.chexing.detail isEqualToString:@"请选择"]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请选择车型";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    
    if ([self.waiguan.detail isEqualToString:@"请选择"]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请选择外观颜色";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    if ([self.neishi.detail isEqualToString:@"请选择"]) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请选择内饰颜色";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    if ([self.cheshen.detail integerValue]==0||[self.cheshen.detail isEqualToString:@""]||self.cheshen.detail==nil) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请输入车身价";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    
    if ([self.youhui.detail isEqualToString:@""]||self.youhui.detail==nil) {
        MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        hud.labelText=@"请输入优惠价";
        hud.mode = MBProgressHUDModeText;
        hud.margin = 10.f;
        hud.removeFromSuperViewOnHide = YES;
        [hud hide:YES afterDelay:1];
        return NO;
    }
    
    return YES;
    
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [self dismissViewControllerAnimated:YES completion:^{
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        
        NSIndexPath *indexpath=[self.tableView indexPathForCell:self.currtcell];
        NSArray *array=self.array[indexpath.section];
        CarpubMode *photomode=array[indexpath.row];
        
        photomode.ischange=YES;
        if (self.button.isSelected) {
            [photomode.imgarray replaceObjectAtIndex:self.button.tag withObject:image];
        }else{
            [photomode.imgarray addObject:image];
        }
        
    }];
    
}

//-(void)pickerBtnClick:(PikerImageTableViewCell *)cell WithBtn:(UIButton *)btn{
//    self.currtcell=cell;
//    if (btn.tag==0) {
//        UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypeCamera;
//        self.picker.sourceType=sourcheType;
//    }else{
//        UIImagePickerControllerSourceType sourcheType = UIImagePickerControllerSourceTypePhotoLibrary;
//        self.picker.sourceType=sourcheType;
//    }
//    [self presentViewController:self.picker animated:YES completion:nil];
//}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.tableView endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 0.1;
    }else{
        return 30;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *lable=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, XHScreenwidth, 30)];
    lable.text=@"购车预算";
    lable.textColor=textnormalGary;
    lable.backgroundColor=bgGarycolor;
    lable.textAlignment=NSTextAlignmentCenter;
    lable.font=[UIFont systemFontOfSize:14];
    if (section==1) {
        return lable;
    }else{
        return nil;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return self.array.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *array=self.array[indexPath.section];
    Pubmode *mode=array[indexPath.row];
    if (mode.block) {
        mode.block();
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *array=self.array[indexPath.section];
    Pubmode *mode=array[indexPath.row];
    if ([mode isKindOfClass:[CarpubMode class]]) {
        CarpubMode *photo=mode;
        
        if (photo.imgarray.count>=4) {
            return 179;
        }else{
            return 111;
        }
    }else{
        if (mode.rowheight!=0){
            return mode.rowheight;
        }else{
            return 44;
        }
        
    }
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    NSArray *array=self.array[section];
    return array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *array=self.array[indexPath.section];
    Pubmode *mode=array[indexPath.row];
    if (mode.titlearray) {
        RadioTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"radio"];
        cell.mode=mode;
        return cell;
    }else if (mode.isEidt){
        EidtTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"editcell"];
        cell.mode=mode;
        cell.delegate=self;
        if (mode.isAluto) {
            cell.detail.enabled=NO;
            cell.price.textColor=mode.yuancolor;
            cell.titlelable.textColor=mode.yuancolor;
        }else{
            if (mode.yuancolor) {
                cell.price.textColor=mode.yuancolor;
                cell.titlelable.textColor=mode.yuancolor;
            }else{
                cell.price.textColor=textnormalGary;
                cell.detail.enabled=YES;
                cell.titlelable.textColor=textnormalGary;
            }
        }
        return cell;
    }else if ([mode isKindOfClass:[CarpubMode class]]){
        PickViewPhotoTableViewCell *picker=[PickViewPhotoTableViewCell cellWithTablView:tableView];
        picker.delegate=self;
        picker.titlelable.x=8;
        picker.mode=mode;
        return picker;
        
    }else if ([mode isKindOfClass:[LongPeizhi class]]){
        
        LongPeizhi *peizhi=mode;
        AddCarPeizhiTableViewCell *cell=[AddCarPeizhiTableViewCell cellWithTableView:tableView];
        
        cell.peizhi=peizhi;
        
        return cell;
    }else if ([mode isKindOfClass:[CarPickModel class]]&&[self.leixing.detail isEqualToString:@"中规"]&&self.mode==nil){
        CarDetailTableViewCell *cell=[CarDetailTableViewCell cellwithTablview:tableView];
        if (self.chexing.cardetail) {
            cell.carname.text=self.chexing.cardetail.name;
            cell.carname.textColor=textnormalGary;
            cell.price.text=[NSString stringWithFormat:@"%@",self.chexing.cardetail.price];
            cell.price.textColor=textnormalGary;
        }else{
            cell.carname.text=nil;
            cell.price.text=@"请选择";
            cell.price.textColor=placehodlerColor;
            cell.carname.textColor=placehodlerColor;
        }
        
        return cell;
    }
    else {
        PublishTopTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"top"];
        cell.titlelable.text=mode.name;
        cell.startlable.hidden=!mode.isMust;
        cell.detail.hidden=mode.isNormal;
        cell.detail.text=mode.detail;
        if ([mode.detail isEqualToString:@"请选择"]||[mode.detail isEqualToString:@"请输入"]) {
            cell.detail.textColor=placehodlerColor;
        }else{
            cell.detail.textColor=textnormalGary;
        }
        if (mode.isNormal) {
            cell.accessoryType=UITableViewCellAccessoryNone;
        }else {
            cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        }
        return cell;
        
    }
}


/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
